package com.shadow.cloud.data.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

/**
 * MybatisPlus分页插件.
 *
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件 3.5.X.
     * 一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     *
     * @return {@link com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor}
     **/
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor()));
        return interceptor;
    }

    /**
     * 设置分页参数.
     *
     * @return {@link com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor}
     **/
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        // 设置最大单页限制数量，默认-1 不受限制，之前的版本默认500
        paginationInterceptor.setMaxLimit(-1L);
        paginationInterceptor.setDbType(DbType.MARIADB);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setOptimizeJoin(true);
        return paginationInterceptor;
    }
}
